﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Caliburn.Micro;
using ThunderboltHelper;
using ThunderboltInterfaceSharp.ViewModels.UserControls.Download;
using ThunderboltInterfaceSharp.Views.UserControls.CloudDisk;
using ThunderboltInterfaceSharp.Views.UserControls.Download;

namespace ThunderboltInterfaceSharp.ViewModels.UserControls.CloudDisk;

public class CloudDiskCatalogViewModel:Screen
{
	public string CloudDiskClassification { get; set; }

	public Screen ContentViewModel { get; set; }
	private CloudDiskCatalogView _cloudDiskCatalogView;

	//ObservableCollection替换为List方便排序
	public List<SingleCloudDiskFolderViewModel> CloudDiskFolderCollection { get; set; }
	public CloudDiskAllFilesViewModel AllFilesItems { get; set; }
	public DownloadingItemsViewModel DownloadingItems { get; set; }

	public Screen ViewModelScreen { get; set; }

	public AddDownloadCommandViewModel AddDownloadCommand { get; set; }
	public bool IsCloudDiskProgressShow { get; set; }
	public bool IsSortFunctionShow { get; set; }
	public CloudDiskCatalogViewModel()
	{
		AllFilesItems = new CloudDiskAllFilesViewModel();
		DownloadingItems = new DownloadingItemsViewModel();
		ViewModelScreen = AllFilesItems;
		CloudDiskFolderCollection = new List<SingleCloudDiskFolderViewModel>();
		CloudDiskClassification = "全部文件";
	
		AddDownloadCommand = new AddDownloadCommandViewModel();
		IsMenuShow = false;
	}
	//todo:ContextMenu样式：
	//圆角
	//用于放图标的左间距
	//字体颜色
	//分割线贯穿
	//弹出位置可以自定义
	public bool IsMenuShow { get; set; }
	public void BtnOtherPreviewLeftDown(object sender, MouseButtonEventArgs e)
	{
		var view = sender as CloudDiskCatalogView;
		if (view.MenuOther.IsOpen)
		{
			view.MenuOther.IsOpen = false;
		}
		else
		{
			view.MenuOther.IsOpen = true;
		}
		//目标
		view.MenuOther.PlacementTarget = view.BtnOther;
		//位置
		view.MenuOther.Placement = PlacementMode.Left;
		//IsMenuShow = !IsMenuShow;
		////显示菜单
		//view.menu.IsOpen = true;
		//_isMenuShow = true;
	}

	public void BtnOtherPreviewRightDown(object sender, MouseButtonEventArgs e)
	{
			e.Handled = true;
	
	}
	public void BtnOtherRightDown(object sender, MouseButtonEventArgs e)
	{
		e.Handled = true;
	}
	public void BtnContextMenuOpening(object sender, ContextMenuEventArgs  e)
	{
		e.Handled = true;
	}

	public void BtnNewDownload()
	{
		var windowManager = IoC.Get<IWindowManager>();
		windowManager.ShowWindowAsync(AddDownloadCommand);
	}
	public void BtnDownloadContentSortClick()=> IsSortFunctionShow=!IsSortFunctionShow;
	/// <summary> 排序方法</summary>
	public void ItemsSort()
	{
		SortedSet<string> listsSortedSet = new SortedSet<string>(MyStringComparer.Instance);
		CloudDiskFolderCollection.ForEach(s=> listsSortedSet.Add(s.ItemName));
		var temp = new List<SingleCloudDiskFolderViewModel>();
		foreach (string item in listsSortedSet)
		{
			if(string.IsNullOrEmpty(item)) continue;
			
			temp.Add(CloudDiskFolderCollection.FirstOrDefault(s => s.ItemName.Equals(item))??new SingleCloudDiskFolderViewModel());
		}
		CloudDiskFolderCollection = temp;
	}


	public void SwitchChildContent(CloudDiskClassification title)
	{
		//ViewModelScreen = title switch
		//{
		//	CloudDiskClassification.Downloading => DownloadingItems,
		//	CloudDiskClassification.Completed => CompletedItems,
		//	_ => DownloadingItems
		//};
		//DownloadClassification = title switch
		//{
		//	CloudDiskClassification.Downloading when DownloadingItems.DownloadItemCollection.Count > 0 => "下载中",
		//	CloudDiskClassification.Downloading when DownloadingItems.DownloadItemCollection.Count == 0 => "已暂停",
		//	CloudDiskClassification.Completed => "已完成",
		//	CloudDiskClassification.CloudAddition=>"云添加",
		//	CloudDiskClassification.FilmVault => string.Empty,
		//	CloudDiskClassification.RecycleBin => "回收站",
		//	CloudDiskClassification.DownloadingPersonal => string.Empty,
		//	CloudDiskClassification.CompletedPersonal => string.Empty,

		//	_ => "下载中"
		//};
		
	}
	protected override void OnViewLoaded(object view)
	{
		base.OnViewLoaded(view);
		_cloudDiskCatalogView = view as CloudDiskCatalogView;
	}

	private Point _offsetPoint;
	public void ButtonUnfoldClick(object sender, MouseButtonEventArgs e)
	{
		_offsetPoint = e.GetPosition(_cloudDiskCatalogView.MainGrid);
		_offsetPoint.X = _cloudDiskCatalogView.MainGrid.ActualWidth - _offsetPoint.X;
		_offsetPoint.Y = _cloudDiskCatalogView.MainGrid.ActualHeight - _offsetPoint.Y;
		if (!IsCloudDiskProgressShow)
		{
			var doubleAnimationW = new DoubleAnimation()
			{
				Duration = new Duration(TimeSpan.FromMilliseconds(200)),
				From = 0,
				To =500,
			};
			var doubleAnimationH = new DoubleAnimation()
			{
				Duration = new Duration(TimeSpan.FromMilliseconds(200)),
				From = 0,
				To =400,
			};

			Storyboard.SetTargetName(doubleAnimationW, _cloudDiskCatalogView.CloudDiskProgressGrid.Name);
			Storyboard.SetTargetProperty(doubleAnimationW, new PropertyPath("Width"));
			Storyboard.SetTargetName(doubleAnimationH, _cloudDiskCatalogView.CloudDiskProgressGrid.Name);
			Storyboard.SetTargetProperty(doubleAnimationH, new PropertyPath("Height"));
			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Children.Add(doubleAnimationW);
			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Children.Add(doubleAnimationH);

			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Begin(_cloudDiskCatalogView);
			IsCloudDiskProgressShow = true;
		}
		else
		{
			var doubleAnimationW = new DoubleAnimation()
			{
				Duration = new Duration(TimeSpan.FromMilliseconds(200)),
				From = 500,
				To = 0,
			};
			var doubleAnimationH = new DoubleAnimation()
			{
				Duration = new Duration(TimeSpan.FromMilliseconds(200)),
				From =400,
				To = 0,
			};

			Storyboard.SetTargetName(doubleAnimationW, _cloudDiskCatalogView.CloudDiskProgressGrid.Name);
			Storyboard.SetTargetProperty(doubleAnimationW, new PropertyPath("Width"));
			Storyboard.SetTargetName(doubleAnimationH, _cloudDiskCatalogView.CloudDiskProgressGrid.Name);
			Storyboard.SetTargetProperty(doubleAnimationH, new PropertyPath("Height"));
			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Children.Add(doubleAnimationW);
			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Children.Add(doubleAnimationH);

			_cloudDiskCatalogView.CloudDiskProgressStoryboard.Begin(_cloudDiskCatalogView);
			Task.Delay(100).ContinueWith((t) =>
			{
				IsCloudDiskProgressShow = false;
			});
		}
		//这句代码放哪里有讲究
	

	}

	public void CloudDiskProgressPopupOpened(object sender, EventArgs e)
	{
		//SetMenuDropAlignment();
		
		
		_cloudDiskCatalogView.CloudDiskProgressPopup.VerticalOffset = _offsetPoint.X;

		_cloudDiskCatalogView.CloudDiskProgressPopup.VerticalOffset = _offsetPoint.Y;
	}
	/// <summary>
	/// 设置弹出菜单相对于相应菜单项的右边对齐
	/// </summary>
	public void SetMenuDropAlignment()
	{
		//先看左对齐右对齐区别改一下条件就行
		// 如果，默认值是左对齐就进行处理
		if (SystemParameters.MenuDropAlignment)
		{
			var type = typeof(SystemParameters);
			var fieldName = "_menuDropAlignment";
			var bindingAttr = BindingFlags.NonPublic | BindingFlags.Static;
			if (type.GetField(fieldName, bindingAttr) is FieldInfo field)
			{
				field.SetValue(null, false);
				return;
			}

			throw new Exception($"无法设置 {nameof(SystemParameters.MenuDropAlignment)} 的值");
		}
	}
}
public enum CloudDiskClassification : byte
{
	AllFile = 0,
	Recent,
	Video,
	Images,
	MySHare,
	RecycleBin
}